package com.yd.mvc.pc.appPicController;

import com.yd.app.service.pc.appPicService.AppPicService;
import com.yd.app.service.sys.UserService;
import com.yd.app.util.BusinessException;
import com.yd.domain.sys.AppPic;
import com.yd.domain.sys.DicEnum;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * Created by sxh on 2018/6/5.
 */
@RestController
public class AppPicController extends BaseController {
    @Autowired
    AppPicService appPicService;
    @Autowired
    UserService userService;

    /**查询列表
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/dapi/appPic/getList", method = RequestMethod.GET)
    public List<AppPic> getAppPicList() throws BusinessException {
        List<AppPic> list = null;
        try {
            list = appPicService.getAppPicList();
        } catch (Exception e) {
            throw new BusinessException(500, "获取App首页轮播图片信息失败");
        }
        return list;
    }

    /**
     * app查询列表
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/mapi/appPic/getList", method = RequestMethod.GET)
    public List<AppPic> getMAppPicList() throws BusinessException {
        List<AppPic> list = null;
        try {
            list = appPicService.getAppPicList();
        } catch (Exception e) {
            throw new BusinessException(500, "获取App首页轮播图片信息失败");
        }
        return list;
    }

    /**
     * 删除代理信息
     * @param appPicId
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/appPic/delete/{appPicId}", method = RequestMethod.DELETE)
    public void deleteAppPicById(@PathVariable("appPicId")String appPicId) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.APPPIC_MANAGER);//验证权限
        }
        appPicService.deleteAppPicById(appPicId);
    }

    /**
     * 添加代理商
     * @param appPic
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/appPic/add", method = RequestMethod.POST)
    public void addAppPic(@RequestBody AppPic appPic) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.APPPIC_MANAGER);//验证权限
        }
        appPicService.addAppPic(appPic);
    }
    /**
     * 修改代理商
     * @param appPic
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/appPic/update", method = RequestMethod.POST)
    public void updateAppPic(@RequestBody AppPic appPic) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.APPPIC_MANAGER);//验证权限
        }
        appPicService.updateAppPic(appPic);
    }
}
